limsEquipment.detailPage = {

    getBtnToolbar: function () {
        var _this = this
        return {
            type: 'btnToolbar',
            items: [{
                type: 'button',
                text: 'GIKAM.BUTTON.BACK',
                icon: 'back',
                onClick: function () {
                    workspace.window.goBack();
                }
            },]
        }
    },

    getBaseInfoTab: function (row) {
        var _this = this
        return {
            type: 'tab',
            panels: [{
                title: 'BASEMODULE.RM.EQUIPMENTS.TAB.BASE_INFO',
                items: [{
                    type: 'form',
                    service: 'limsEquipmentServiceImpl',
                    dbTable: 'T_LIMS_EQUIPMENT',
                    id: _this.idManager.equipForm,
                    fields: limsEquipment.getBaseInfoFormFields(),
                    onUpdated: function () {
                        var grid = Gikam.getComp(_this.idManager.equipGrid)
                        grid.refreshRowById(grid.getActivedRow().id)
                    },
                    url: Gikam.printf(limsEquipment.baseUrl + '/{id}', {
                        id: row.id
                    })
                }]
            }, {// 配件记录
                title: 'T_LIMS_EQUIPMENT_ACCESSORIES.SERVICE_NAME',
                items: [{
                    type: 'grid',
                    id: _this.idManager.equipAccessoriesGrid,
                    height: 300,
                    columns: limsEquipment.getAccessoriesEditListColumns(),
                    service: 'limsEquipmentAccessoriesServiceImpl',
                    onLoadSuccess: function (rows) {
                        if (Gikam.isNotEmpty(rows)) {
                            this.activeRowByIndex(0)
                        } else {
                            _this.refreshAttachments(_this.idManager.equipAccessoriesGrid)
                        }
                    },
                    onRowActive: function (index, row) {
                        _this.refreshAttachments(_this.idManager.equipAccessoriesGrid)
                    },
                    deleteFormatter: function (row) {
                        return row.name
                    },
                    url: _this.urlManager.equipAccessoriesGrid + '/queries',
                    requestData: {
                        equipmentId_EQ: row.id
                    },
                    toolbar: [{
                        type: 'button',
                        text: 'GIKAM.BUTTON.INSERT',
                        icon: 'add',
                        onClick: function () {
                            let equipGrid = Gikam.getComp(_this.idManager.equipGrid)
                            let actRow = equipGrid.getActivedRow()
                            if (Gikam.isEmpty(actRow)) {
                                Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM')
                                return
                            }
                            Gikam.preInsert({
                                modalTitle: 'BASEMODULE.RM.EQUIPMENTS.MODAL.ADD',
                                fields: limsEquipment.getAccessoriesBaseInfoFormFields(),
                                disableNotEmptyValidate: true,
                                url: _this.urlManager.equipAccessoriesGrid,
                                param: {
                                    equipmentId: actRow.id
                                }
                            }).done(function (id) {
                                Gikam.getComp(_this.idManager.equipAccessoriesGrid).refresh()
                            })
                        }
                    }, {
                        type: 'button',
                        text: 'GIKAM.BUTTON.DELETE',
                        icon: 'remove',
                        onClick: function () {
                            Gikam.getComp(_this.idManager.equipAccessoriesGrid).deleteRows(_this.urlManager.equipAccessoriesGrid)
                        }
                    }]
                }, _this.getAttachmantStruct(_this.idManager.equipAccessoriesGrid, 'T_LIMS_EQUIPMENT_ACCESSORIES')]
            }, {// 验收确认记录
                title: 'T_LIMS_EQUIPMENT_ACCEPTANCE.SERVICE_NAME',
                items: [{
                    type: 'grid',
                    id: _this.idManager.equipAcceptanceGrid,
                    height: 300,
                    columns: limsEquipment.getAcceptanceEditListColumns(),
                    service: 'limsEquipmentAcceptanceServiceImpl',
                    onLoadSuccess: function (rows) {
                        if (Gikam.isNotEmpty(rows)) {
                            this.activeRowByIndex(0)
                        } else {
                            _this.refreshAttachments(_this.idManager.equipAcceptanceGrid)
                        }
                    },
                    onRowActive: function (index, row) {
                        _this.refreshAttachments(_this.idManager.equipAcceptanceGrid)
                    },
                    deleteFormatter: function (row) {
                        return row.cause
                    },
                    url: _this.urlManager.equipAcceptanceGrid + '/queries',
                    requestData: {
                        equipmentId_EQ: row.id
                    },
                    toolbar: [{
                        type: 'button',
                        text: 'GIKAM.BUTTON.INSERT',
                        icon: 'add',
                        onClick: function () {
                            let equipGrid = Gikam.getComp(_this.idManager.equipGrid)
                            let actRow = equipGrid.getActivedRow()
                            if (Gikam.isEmpty(actRow)) {
                                Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM')
                                return
                            }
                            Gikam.preInsert({
                                modalTitle: 'BASEMODULE.RM.EQUIPMENTS.MODAL.ADD',
                                fields: limsEquipment.getAcceptanceBaseInfoFormFields(),
                                disableNotEmptyValidate: true,
                                url: _this.urlManager.equipAcceptanceGrid,
                                param: {
                                    equipmentId: actRow.id
                                }
                            }).done(function (id) {
                                Gikam.getComp(_this.idManager.equipAcceptanceGrid).refresh()
                            })
                        }
                    }, {
                        type: 'button',
                        text: 'GIKAM.BUTTON.DELETE',
                        icon: 'remove',
                        onClick: function () {
                            Gikam.getComp(_this.idManager.equipAcceptanceGrid).deleteRows(_this.urlManager.equipAcceptanceGrid)
                        }
                    }]
                }, _this.getAttachmantStruct(_this.idManager.equipAcceptanceGrid, 'T_LIMS_EQUIPMENT_ACCEPTANCE')]
            }, {
                title: 'T_LIMS_EQUIPMENT_NEWMAINTAIN.SERVICE_NAME',
                items: [{
                    type: 'grid',
                    id: _this.idManager.equipNewMaintainGrid,
                    height: 300,
                    columns: limsEquipment.getNewMaintainEditListColumns(),
                    service: 'limsEquipmentNewmaintainServiceImpl',
                    onLoadSuccess: function (rows) {
                        if (Gikam.isNotEmpty(rows)) {
                            this.activeRowByIndex(0)
                        } else {
                            _this.refreshAttachments(_this.idManager.equipNewMaintainGrid)
                        }
                    },
                    onRowActive: function (index, row) {
                        _this.refreshAttachments(_this.idManager.equipNewMaintainGrid)
                    },
                    deleteFormatter: function (row) {
                        return row.cause
                    },
                    url: _this.urlManager.equipNewMaintainGrid + '/queries',
                    requestData: {
                        equipmentId_EQ: row.id
                    },
                    toolbar: [{
                        type: 'button',
                        text: 'GIKAM.BUTTON.INSERT',
                        icon: 'add',
                        onClick: function () {
                            let equipGrid = Gikam.getComp(_this.idManager.equipGrid)
                            let actRow = equipGrid.getActivedRow()
                            if (Gikam.isEmpty(actRow)) {
                                Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM')
                                return
                            }
                            Gikam.preInsert({
                                modalTitle: 'BASEMODULE.RM.EQUIPMENTS.MODAL.ADD',
                                fields: limsEquipment.getNewMaintainBaseInfoFormFields(),
                                disableNotEmptyValidate: true,
                                url: _this.urlManager.equipNewMaintainGrid,
                                param: {
                                    equipmentId: actRow.id
                                }
                            }).done(function (id) {
                                Gikam.getComp(_this.idManager.equipNewMaintainGrid).refresh()
                            })
                        }
                    }, {
                        type: 'button',
                        text: 'GIKAM.BUTTON.DELETE',
                        icon: 'remove',
                        onClick: function () {
                            var selections = Gikam.getComp(_this.idManager.equipNewMaintainGrid).getSelections()
                            for (let i = 0; i < selections.length; i++) {
                                if (selections[i].status === 'done') {
                                    Gikam.alert("EXISTS.COMPLATED.RECORD.PLEASE.CHOOSE.AGAIN")
                                    return false
                                }
                            }
                            Gikam.getComp(_this.idManager.equipNewMaintainGrid).deleteRows(_this.urlManager.equipNewMaintainGrid)
                        }
                    }]
                }, _this.getAttachmantStruct(_this.idManager.equipNewMaintainGrid, 'T_LIMS_EQUIPMENT_NEWMAINTAIN')]
            }, {
                title: 'T_LIMS_EQUIPMENT_MEASURE.SERVICE_NAME',// 计量记录
                items: [{
                    type: 'grid',
                    id: _this.idManager.equipMeasureGrid,
                    height: 300,
                    columns: limsEquipment.getMeasureEditListColumns(),
                    service: 'limsEquipmentMeasureServiceImpl',
                    onLoadSuccess: function (rows) {
                        if (Gikam.isNotEmpty(rows)) {
                            this.activeRowByIndex(0)
                        } else {
                            _this.refreshAttachments(_this.idManager.equipMeasureGrid)
                        }
                    },
                    onRowActive: function (index, row) {
                        _this.refreshAttachments(_this.idManager.equipMeasureGrid)
                    },
                    deleteFormatter: function (row) {
                        return row.cycle
                    },
                    url: _this.urlManager.equipMeasureGrid + '/queries',
                    requestData: {
                        equipmentId_EQ: row.id
                    },
                    toolbar: [{
                        type: 'button',
                        text: 'GIKAM.BUTTON.INSERT',
                        icon: 'add',
                        onClick: function () {
                            let equipGrid = Gikam.getComp(_this.idManager.equipGrid)
                            let actRow = equipGrid.getActivedRow()
                            if (Gikam.isEmpty(actRow)) {
                                Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM')
                                return
                            }
                            Gikam.preInsert({
                                modalTitle: 'BASEMODULE.RM.EQUIPMENTS.MODAL.ADD',
                                fields: limsEquipment.getMeasureBaseInfoFormFields(),
                                disableNotEmptyValidate: true,
                                url: _this.urlManager.equipMeasureGrid,
                                param: {
                                    equipmentId: actRow.id
                                }
                            }).done(function (id) {
                                Gikam.getComp(_this.idManager.equipMeasureGrid).refresh()
                            })
                        }
                    }, {
                        type: 'button',
                        text: 'GIKAM.BUTTON.DELETE',
                        icon: 'remove',
                        onClick: function () {
                            var selections = Gikam.getComp(_this.idManager.equipMeasureGrid).getSelections()
                            for (let i = 0; i < selections.length; i++) {
                                if (selections[i].measureStatus === 'done') {
                                    Gikam.alert("EXISTS.COMPLATED.RECORD.PLEASE.CHOOSE.AGAIN")
                                    return false
                                }
                            }
                            Gikam.getComp(_this.idManager.equipMeasureGrid).deleteRows(_this.urlManager.equipMeasureGrid)
                        }
                    }]
                }, _this.getAttachmantStruct(_this.idManager.equipMeasureGrid, 'T_LIMS_EQUIPMENT_MEASURE')]
            }, {
                title: 'T_LIMS_EQUIPMENT_MAINTAIN.SERVICE_NAME',// 维保记录
                items: [{
                    type: 'grid',
                    id: _this.idManager.equipMaintainGrid,
                    height: 300,
                    columns: limsEquipment.getMaintainEditListColumns(),
                    service: 'limsEquipmentMaintainServiceImpl',
                    onLoadSuccess: function (rows) {
                        if (Gikam.isNotEmpty(rows)) {
                            this.activeRowByIndex(0)
                        } else {
                            _this.refreshAttachments(_this.idManager.equipMaintainGrid)
                        }
                    },
                    onRowActive: function (index, row) {
                        _this.refreshAttachments(_this.idManager.equipMaintainGrid)
                    },
                    deleteFormatter: function (row) {
                        return row.maintainCycle
                    },
                    url: _this.urlManager.equipMaintainGrid + '/queries',
                    requestData: {
                        equipmentId_EQ: row.id
                    },
                    toolbar: [{
                        type: 'button',
                        text: 'GIKAM.BUTTON.INSERT',
                        icon: 'add',
                        onClick: function () {
                            let equipGrid = Gikam.getComp(_this.idManager.equipGrid)
                            let actRow = equipGrid.getActivedRow()
                            if (Gikam.isEmpty(actRow)) {
                                Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM')
                                return
                            }
                            Gikam.preInsert({
                                modalTitle: 'BASEMODULE.RM.EQUIPMENTS.MODAL.ADD',
                                fields: limsEquipment.getMaintainBaseInfoFormFields(),
                                disableNotEmptyValidate: true,
                                url: _this.urlManager.equipMaintainGrid,
                                param: {
                                    equipmentId: actRow.id
                                }
                            }).done(function (id) {
                                Gikam.getComp(_this.idManager.equipMaintainGrid).refresh()
                            })
                        }
                    }, {
                        type: 'button',
                        text: 'GIKAM.BUTTON.DELETE',
                        icon: 'remove',
                        onClick: function () {
                            var selections = Gikam.getComp(_this.idManager.equipMaintainGrid).getSelections()
                            for (let i = 0; i < selections.length; i++) {
                                if (selections[i].status === 'done') {
                                    Gikam.alert("EXISTS.COMPLATED.RECORD.PLEASE.CHOOSE.AGAIN")
                                    return false
                                }
                            }
                            Gikam.getComp(_this.idManager.equipMaintainGrid).deleteRows(_this.urlManager.equipMaintainGrid)
                        }
                    }]
                }, _this.getAttachmantStruct(_this.idManager.equipMaintainGrid, 'T_LIMS_EQUIPMENT_MAINTAIN')]
            }, {
                title: 'T_LIMS_EQUIP_VERIFICATION_RECORD.SERVICE_NAME',
                items: [{
                    type: 'grid',
                    id: _this.idManager.equipVerificationRecordGrid,
                    service: 'limsEquipVerificationRecordServiceImpl',
                    dbTable: 'T_LIMS_EQUIP_VERIFICATION_RECORD',
                    columns: limsEquipment.getVerificationRecordEditListColumns(),
                    deleteFormatter: function (row) {
                        return row.verificationType + '-' + row.verificationDetail
                    },
                    onLoadSuccess: function (data) {
                        if (!Gikam.isEmpty(data)) {
                            this.activeRowByIndex(0)
                        } else {
                            _this.refreshAttachments(_this.idManager.equipVerificationRecordGrid)
                        }
                    },
                    onRowActive: function (index, row) {
                        _this.refreshAttachments(_this.idManager.equipVerificationRecordGrid)
                    },
                    url: _this.urlManager.equipVerificationRecordGrid + '/queries',
                    requestData: {
                        equipmentId_EQ: row.id
                    },
                    height: 300,
                    toolbar: [{
                        type: 'button',
                        text: 'GIKAM.BUTTON.INSERT',
                        icon: 'add',
                        onClick: function () {
                            let equipGrid = Gikam.getComp(_this.idManager.equipGrid)
                            let actRow = equipGrid.getActivedRow()
                            if (Gikam.isEmpty(actRow)) {
                                Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM')
                                return
                            }
                            Gikam.preInsert({
                                modalTitle: 'BASEMODULE.RM.EQUIPMENTS.MODAL.ADD',
                                fields: limsEquipment.getVerificationRecordBaseInfoFormFields(),
                                url: _this.urlManager.equipVerificationRecordGrid,
                                param: {
                                    equipmentId: actRow.id
                                }
                            }).done(function (req) {
                                Gikam.getComp('lims-equip-verification-record-edit-list-grid').refresh()
                            })
                        }
                    }, {
                        type: 'button',
                        text: 'GIKAM.BUTTON.DELETE',
                        icon: 'remove-row',
                        onClick: function () {
                            Gikam.getComp('lims-equip-verification-record-edit-list-grid').deleteRows(_this.urlManager.equipVerificationRecordGrid)
                        }
                    }]
                }, _this.getAttachmantStruct(_this.idManager.equipVerificationRecordGrid, 'T_LIMS_EQUIP_VERIFICATION_RECORD')]
            }, {
                title: 'T_EQUIPMENT.TABLENAME.CHANGE.EQUIPSTATE',
                items: [{
                    type: 'grid',
                    id: _this.idManager.operationRecord,
                    service: 'limsOperationrecordServiceImpl',
                    dbTable: 'T_LIMS_OPERATIONRECORD',
                    columns: limsEquipment.getOperationRecordListColumns(),
                    onLoadSuccess: function (data) {
                    },
                    onRowActive: function (index, row) {
                    },
                    toolbar: [],
                    order: {'operationTime': 'asc'},
                    url: _this.urlManager.operationRecord + '/queries',
                    requestData: {
                        businessId_EQ: row.id
                    }
                }]
            }, {
                title: 'GIKAM.FILE.ATTACHMENT',
                items: [{
                    type: 'uploader',
                    immediateUpload: true,
                    id: _this.getAttachUploaderId(_this.idManager.equipGrid),
                    dbTable: 'T_LIMS_EQUIPMENT',
                    bizId: row.id,
                    filter: false,
                    genericQuery: false,
                    onBeforeUpload: function () {
/*                        var activedRow = Gikam.getComp(gridId).getActivedRow()
                        if (Gikam.isEmpty(activedRow)) {
                            Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM', 200)
                            return false
                        }*/
                    },
                }]
            },]
        }
    },

    getAttachUploaderId: function (gridId) {
        return gridId + '-uploader'
    },

    refreshAttachments: function (gridId) {
        let _this = this
        let grid = Gikam.getComp(gridId)
        let actRow = grid.getActivedRow()
        Gikam.getAsyncComp(_this.getAttachUploaderId(gridId)).done(uploader => {
            if (actRow && actRow.id) {
                uploader.setOptions({
                    bizId: actRow.id,
                })
            } else {
                uploader.setOptions({
                    bizId: -1,
                })
            }
        })
    },

    getAttachmantStruct: function (gridId, dbTable) {
        let _this = this
        return {
            type: 'uploader',
            immediateUpload: true,
            id: _this.getAttachUploaderId(gridId),
            dbTable: dbTable,
            // bizId: -1,
            filter: false,
            genericQuery: false,
            onBeforeUpload: function () {
                var activedRow = Gikam.getComp(gridId).getActivedRow()
                if (Gikam.isEmpty(activedRow)) {
                    Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM', 200)
                    return false
                }
            },
        }
    },

    idManager: {
        equipGrid: 'lims-equipment-edit-list-grid',
        equipForm: 'lims-equipment-edit-list-base-info-form',
        equipVerificationRecordGrid: 'lims-equip-verification-record-edit-list-grid',
        equipMaintainGrid: 'lims-equipment-maintain-edit-list-grid',
        equipMeasureGrid: 'lims-equipment-measure-edit-list-grid',
        equipNewMaintainGrid: 'lims-equipment-newmaintain-edit-list-grid',
        equipAcceptanceGrid: 'lims-equipment-acceptance-edit-list-grid',
        equipAccessoriesGrid: 'lims-equipment-accessories-edit-list-grid',
        operationRecord: IFM_CONTEXT + 'lims-equipment-operation-record-list-grid',
    },

    urlManager: {
        equipVerificationRecordGrid: IFM_CONTEXT + '/secure/basemodule/rm/equip-verification-records',
        equipMaintainGrid: IFM_CONTEXT + '/secure/basemodule/rm/equipment-maintains',
        equipMeasureGrid: IFM_CONTEXT + '/secure/basemodule/rm/equipment-measures',
        equipNewMaintainGrid: IFM_CONTEXT + '/secure/basemodule/rm/equipment-newmaintains',
        equipAcceptanceGrid: IFM_CONTEXT + '/secure/basemodule/rm/equipment-acceptances',
        equipAccessoriesGrid: IFM_CONTEXT + '/secure/basemodule/rm/equipment-accessoriess',
        operationRecord: IFM_CONTEXT + '/secure/basemodule/basedata/operationrecords',
    },

    createPage: function () {
        let _this = this
        Gikam.create('layout', {
            center: {
                items: [this.getBtnToolbar(), this.getBaseInfoTab({id: _this.param.id})]
            },
            toolbarAlign: 'right',
            renderTo: workspace.window.$dom
        });
        return this;
    },

    init: function (param) {
        this.param = param
        this.createPage()
    }
}
